package com.fare.taluo.core.domain.thread;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentInformation;
import org.apache.pdfbox.util.PDFTextStripper;

import com.fare.taluo.conf.PropertyUtils;
import com.fare.taluo.core.domain.config.city.PortEntity;
import com.fare.taluo.core.domain.config.line.FlightInfoEntity;
import com.fare.taluo.core.domain.crawl.CrawlPropertiesLoader;
import com.fare.taluo.core.domain.crawl.adapter.sub.ZHAirLinePdfAdaptor;
import com.fare.taluo.util.DateUtils;

public class ZHLinePdfParseThread extends Thread{
	
	@Override
	public void run() {
		
		PortEntity entity =  new PortEntity();
		InputStream input = null;	
		ZHAirLinePdfAdaptor adap = new ZHAirLinePdfAdaptor(null);
		String url = "http://res.shenzhenair.com/b2c/upfiles/20130415/File/13660094202362116672163.pdf";
		File pdfFile = null;
		
		int fetchCount = PropertyUtils.getIntProperty("crawl.line.fail.repeat.times", CrawlPropertiesLoader.CONFIG_FILE);
		try{
			while(fetchCount > 0){
				// HNAirLinePdfAdaptor adap = new HNAirLinePdfAdaptor(null);
				// File file = (File)adap.fetch("http://www.hnair.com/lxxx/hbskb/201005/P020130603327385934157.pdf");
				pdfFile = (File) adap.fetch(url); 
				System.out.println("the length of file is "+pdfFile.length());
				 if(pdfFile.length()>40000){
					 break;
				 }
		      fetchCount--;
		    }
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		
		PDDocument document = null;
		Map<String,PortEntity> allPorts = null;
		try {
			allPorts = entity.findPortEntityToMapByName();
			input = new FileInputStream(pdfFile);

			// 加载 pdf 文档

			document = PDDocument.load(input);

			/** 文档属性信息 **/

			PDDocumentInformation info = document.getDocumentInformation();

			// 获取内容信息
            String companyCode = "ZH";
			PDFTextStripper pts = new PDFTextStripper();
			String content = pts.getText(document);
			List<List> listlist = new ArrayList<List>();
			// 根据"ZH"将字符串分隔
			String[] records = content.split(companyCode);
			// 从第二个字符串开始计算
			for (int i = 1; i < records.length; i++) {
				// 获得单条记录
				String str = records[i].replaceAll("\r\n", "");
				String newString = companyCode + str;
				String[] arrayString = newString.split(" ");
				List<String> listOne = new ArrayList<String>();
				List<String> listTwo = new ArrayList<String>();
				String companyCodeOne = null;
				String companyCodeTwo = null;
				if (arrayString[0].contains("/")) {
					String[] companyCodes = arrayString[0].split("/");
					companyCodeOne = companyCodes[0];
					if (companyCodes[1].length() > 1) {
						companyCodeTwo = companyCodes[0].substring(0,
								companyCodes[0].length() - 2) + companyCodes[1];
					} else {
						companyCodeTwo = companyCodes[0].substring(0,
								companyCodes[0].length() - 1) + companyCodes[1];
					}

				} else {
					companyCodeOne = arrayString[0];
					companyCodeTwo = arrayString[0];
				}

				listOne.add(companyCodeOne);
				listOne.add(arrayString[1]);
				listOne.add(arrayString[2]);
				listOne.add(arrayString[3]);
				listOne.add(arrayString[4]);
				listOne.add(arrayString[5]);

				listTwo.add(companyCodeTwo);
				listTwo.add(arrayString[1]);
				listTwo.add(arrayString[5]);
				listTwo.add(arrayString[6]);
				listTwo.add(arrayString[7]);
				listTwo.add(arrayString[8]);

				listlist.add(listOne);
				listlist.add(listTwo);

			}		
			for (Iterator iterator = listlist.iterator(); iterator.hasNext();) {
				List<String> list = (List) iterator.next();
				FlightInfoEntity flightEntity = new FlightInfoEntity();
				PortEntity portVO = allPorts.get(list.get(2));
				PortEntity portVOToCity =  allPorts.get(list.get(5));
				if(portVO != null && portVOToCity != null){
					flightEntity.setFlightNo(list.get(0)); 
					 flightEntity.setFrequence(list.get(1).replaceAll("[.]", ""));
					 
					 if(list.get(2).equals("北京")){
						 flightEntity.setFromCityName(list.get(2)+"(PEK)");
					 }else{
						 flightEntity.setFromCityName(list.get(2));
					 }
					
					 if(list.get(3).toCharArray().length>4){
						 flightEntity.setFlightStartTime(list.get(3));
					 }else{
						 flightEntity.setFlightStartTime("0"+list.get(3));
					 }
					 if(list.get(4).toCharArray().length>4){
						 flightEntity.setFlightEndTime(list.get(4));
					 }else{
						 flightEntity.setFlightEndTime("0"+list.get(4));
					 }
					 if(list.get(5).equals("北京")){
						 flightEntity.setToCityName(list.get(5)+"(PEK)");
					 }else{
						 flightEntity.setToCityName(list.get(5));
					 }
					 
					 if(portVO.getCode().equals("NAY")){
						 flightEntity.setFromCity("PEK");
					 }else{
						 flightEntity.setFromCity(portVO.getCode());
					 }
					 
					 try {
						 if(portVOToCity.getCode().equals("NAY")){
							 flightEntity.setToCity("PEK");
						 }else{
							 flightEntity.setToCity(portVOToCity.getCode());
						 }
						 
					} catch (Exception e) {
						e.printStackTrace();
					}
					
					 flightEntity.setCompanyCode(companyCode);
					 flightEntity.setCompanyName("深圳航空");
					 flightEntity.setEntryTime(new Date());
					 flightEntity.setFlightDate(DateUtils.dateToStr(new Date(), "MM-dd"));
					 try {
						 flightEntity.tranSave(flightEntity);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			
			}
			System.out.println("---------finished---------");
			ThreadFlag.setIsFinish(true);
		} catch (Exception e){

			e.printStackTrace();
		} finally {

			if (null != input)
				try {
					input.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			if (null != document)
				try {
					document.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

		}
	}
	
}
